package com.qin.controller;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qin.entity.Book;
import com.qin.entity.BookCategory;
import com.qin.service.BookCategoryService;
import com.qin.service.BookService;
import com.qin.service.impl.BookCategoryServiceImpl;
import com.qin.service.impl.BookServiceImpl;

import cn.hutool.log.StaticLog;

/**
 * 负责处理/和/index请求，来到首页
 */
@WebServlet({"/index",""})
public class IndexServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private BookService bookService = new BookServiceImpl();
 	private BookCategoryService bookCategoryService = new BookCategoryServiceImpl();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		StaticLog.debug("欢迎来到index首页");
		// 查出所有的一级分类
		List<BookCategory> categoryByCLevel = bookCategoryService.queryBookCategoryByCLevel(1);
		// 查出所有四大名著
		List<Book> masterWork = bookService.getMasterWork();
		// 随机查出十本推荐图书
		List<Book> recommendBooks = bookService.getRecommendBooks(10);
		// 查出图书、电子书、网络文学并打乱顺序只要前八条数据
		List<Book> content = bookService.getBooksByCategoryLevel1Id(1L);
		if(content != null) {
			// 先拿到图书榜：对效率排序后，拿到前十条数据
			List<Book> collect = content.stream().sorted().limit(10).collect(Collectors.toList());
			request.setAttribute("contentRank", collect);
			
			// 打乱后获取前八条数据
			Collections.shuffle(content);
			List<Book> contentLEight = content.stream().limit(8).collect(Collectors.toList());
			
			request.setAttribute("contentLEight", contentLEight);
		}else {
			request.setAttribute("contentLEight", null);
		}
		
		List<Book> ebook = bookService.getBooksByCategoryLevel1Id(3L);
		if(ebook != null) {
			Collections.shuffle(ebook);
			List<Book> ebookLEight = ebook.stream().limit(8).collect(Collectors.toList());
			request.setAttribute("ebookLEight", ebookLEight);
		}else {
			request.setAttribute("ebookLEight", null);
		}
		
		List<Book> obook = bookService.getBooksByCategoryLevel1Id(3L);
		if(ebook != null) {
			Collections.shuffle(obook);
			List<Book> obookLEight = obook.stream().limit(8).collect(Collectors.toList());
			request.setAttribute("obookLEight", obookLEight);
		}else {
			request.setAttribute("obookLEight", null);
		}
		
		
		if(categoryByCLevel != null) {
			Map<Integer, List<BookCategory>> collect = categoryByCLevel.stream().collect(Collectors.groupingBy(BookCategory::getLabelId));
			
			request.setAttribute("bookCategoryByLabel", collect);
		}
		
		// 放在请求域中
		request.setAttribute("categoryByCLevel", categoryByCLevel);
		request.setAttribute("masterwork", masterWork);
		request.setAttribute("recommendBooks", recommendBooks);
		// 请求转发给首页
		request.getRequestDispatcher("/WEB-INF/index.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
